* Replication code for Hirst & Robertson (2022)
* File 4 of 4 
* This file was written for Stata 16.1 and has not been tested on any other version.
* Please read the README file before continuing. 

clear all
cd "C:\your\data\directory\" //Change this to where you are storing the files
local date FINAL

/*INTERMEDIATE DATA IN 
"ISS_augmented" //from 01_PrepareRecordDate 
"MeetingLevel" //from 01_PrepareRecordDate 
*/



********************
*** Make Table 4 ***
********************
if 1 == 1 {
	// We will need CompanyID again, so we go back to the full ISS data to get it 
	use "ISS_augmented.dta", clear 
	keep CompanyID MeetingID 
	duplicates drop 
	tempfile temp 
	save `temp', replace 

	//Now we go to the meeting level file 
	use "MeetingLevel", clear
	keep MeetingID MeetingType Recorddate MeetingDate //Keep the variables we need 
	duplicates drop 
	duplicates r MeetingID  
	local uniq = r(unique_value) 
	qui count 
	assert `uniq' == r(N)

	//Merge these together 
	merge 1:1 MeetingID using `temp' // This gets our company identifiers

	//Now tidy things up 
	drop if _merge == 2
	qui count if _merge == 1
	assert r(N) == 0 // Everything in the meeting file merges
	drop _merge 
	
	keep if MeetingType == "Annual" // We only want annual meetings
	
	duplicates r MeetingType Recorddate MeetingDate CompanyID // NOTE: turns out there are some issues -- a company has different MeetingIDs for meetings on the same date with the same record date -- we will get rid of these  
	bysort MeetingType Recorddate MeetingDate CompanyID : keep if _n == 1 
	
	bysort CompanyID (Recorddate): gen N = _n 
	bysort CompanyID (Recorddate): gen Recorddate_1 = Recorddate[_n-1]
	format Recorddate_1  %td
	order CompanyID Recorddate Recorddate_1 N 
	
	//Prediction 1: Last year's record date + 356 
	gen prediction_1 = Recorddate_1 + 365 
	format prediction_1 %td
	gen diff_1 = Recorddate - prediction_1
*	sum diff_1, det


	forvalues j = 1/1 {
		foreach i in 1 3 5 7 10 14 {
			gen pred`j'_`i' = 0 
			replace pred`j'_`i' = 1 if abs(diff_`j') <= `i' 
			replace pred`j'_`i' = . if diff_`j' == . 
			replace pred`j'_`i' = . if abs(diff_`j') >= 350
			}
		}
	forvalues j = 1/1 {
		foreach i in 1 3 5 7 10 14 {
			gen pred`j'_`i'_v1 = pred`j'_`i' 
			replace pred`j'_`i'_v1 = . if abs(diff_`j') >= 182
			}
		}		 
	forvalues j = 1/1 {
		sum pred`j'_* 
		}
		
	forvalues j = 1/1 {
		foreach i in 1 3 5 7 10 14 {
			sum pred`j'_`i'_v1 
			}	
		}	 		
*Make the table 
local dates 1 3 5 7 10 14 

mat def B=J(6,2,.)
	local j = 1 
	foreach i in `dates' {
		mat B[`j',1] = `i' 
		sum pred1_`i'
		mat B[`j',2] = r(mean) 
		local j = `j' + 1 
		}

	putexcel set "Results/Table4_`date'", replace
	putexcel A1= "Window (in days)"
	putexcel B1= "Percentage of Record Dates"
	putexcel A2 = mat(B) 
			
	}
